package labox.innovation.gameserver.handler;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;

/**
 * 此类管理了GM命令的处理逻辑对象
 *
 * @version $Revision: 1.1.4.5 $ $Date: 2005/03/27 15:30:09 $
 */
public class AdminCommandHandler {
	
	private static Logger _log = LoggerFactory.getLogger(AdminCommandHandler.class.getName());

	private Map<String, IAdminCommandHandler> _datatable;

	public static AdminCommandHandler getInstance() {
		return SingletonHolder._instance;
	}

	private AdminCommandHandler() {
		_datatable = new FastMap<String, IAdminCommandHandler>();
	}

	public void registerAdminCommandHandler(IAdminCommandHandler handler) {
		String[] ids = handler.getAdminCommandList();
		for (int i = 0; i < ids.length; i++) {
			if (Config.DEBUG)
				_log.debug("Adding handler for command " + ids[i], Config.SERVER_ID);
			_datatable.put(ids[i], handler);
		}
	}

	public IAdminCommandHandler getAdminCommandHandler(String adminCommand) {
		String command = adminCommand;
		if (adminCommand.indexOf(" ") != -1) {
			command = adminCommand.substring(0, adminCommand.indexOf(" "));
		}
		if (Config.DEBUG)
			_log.debug("getting handler for command: " + command + " -> " + (_datatable.get(command) != null), Config.SERVER_ID);
		return _datatable.get(command);
	}

	/**
	 * @return
	 */
	public int size() {
		return _datatable.size();
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final AdminCommandHandler _instance = new AdminCommandHandler();
	}
}
